//
//  ZSTagButton.m
//  Fangxiaov
//
//  Created by safiri on 2017/3/3.
//  Copyright © 2017年 com.f. All rights reserved.
//

#import "ZSTagButton.h"

@implementation ZSTagButton

+ (instancetype)buttonWithTag:(ZSButtonTag *)tag {
    ZSTagButton *btn = [super buttonWithType:UIButtonTypeCustom];
    
    //1.title
    if (tag.attributedText) {
        [btn setAttributedTitle:tag.attributedText forState:UIControlStateNormal];
    }
    if (tag.text) {
        [btn setTitle:tag.text forState:UIControlStateNormal];
    }
    if (tag.textColor) {
        [btn setTitleColor:tag.textColor forState:UIControlStateNormal];
    }
    
    if (tag.font ) {
        [[btn titleLabel] setFont:tag.font];
    }else if (tag.fontSize > 0) {
        [[btn titleLabel] setFont:[UIFont systemFontOfSize:tag.fontSize]];
    }
    
    if (tag.highlightedTextColor) {
        [btn setTitleColor:tag.highlightedTextColor forState:UIControlStateHighlighted];
    }
    if (tag.slcTextColor) {
        [btn setTitleColor:tag.slcTextColor forState:UIControlStateSelected];
    }
    btn.titleLabel.textAlignment = tag.textAlignment;
    
    //2.背景相关
    if (tag.bgColor) {
        [btn setBackgroundColor:tag.bgColor];
    }
    if (tag.bgImage) {
        [btn setBackgroundImage:tag.bgImage forState:UIControlStateNormal];
    }
    if (tag.nrmBgImageColor) {
        [btn setBackgroundImage:[self imageWithColor:tag.nrmBgImageColor] forState:UIControlStateNormal];
    }
    if (tag.slcBgImageColor) {
        [btn setBackgroundImage:[self imageWithColor:tag.slcBgImageColor] forState:UIControlStateSelected];
    }
    if (tag.highlightedBgImageColor) {
        [btn setBackgroundImage:[self imageWithColor:tag.highlightedBgImageColor] forState:UIControlStateHighlighted];
        [btn setBackgroundImage:[self imageWithColor:tag.highlightedBgImageColor] forState:UIControlStateHighlighted | UIControlStateSelected];
    }
    
    //3.边框
    if (tag.borderColor) {
        btn.layer.borderColor = tag.borderColor.CGColor;
    }
    if (tag.borderWidth > 0) {
        btn.layer.borderWidth = tag.borderWidth;
    }
    if (tag.borderCornerRadius) {
        btn.layer.cornerRadius = tag.borderCornerRadius;
        btn.layer.masksToBounds = YES;
    }
    
    //4.能否被点击
    btn.userInteractionEnabled = tag.enable;
    if (tag.enable && tag.autoDarkNrmBgColorForHighlighted) {
        UIColor *highlightedBgColor = [self darkerColor:tag.nrmBgImageColor];
        [btn setBackgroundImage:[self imageWithColor:highlightedBgColor] forState:UIControlStateHighlighted];
        [btn setBackgroundImage:[self imageWithColor:highlightedBgColor] forState:UIControlStateSelected | UIControlStateHighlighted];
        //设置UIButton isSelected=YES 后再点击时还为此颜色
        //UIControlStateSelected | UIControlStateHighlighted,我们可以理解成选中时候的高亮状态.
    }
    
    //5.固定大小
    if (tag.fixedSize.height > 0 && tag.fixedSize.width > 0) {
        CGRect f = btn.frame;
        f.size = tag.fixedSize;
        btn.frame = f;
    }
    
    btn.titleLabel.adjustsFontSizeToFitWidth = tag.adjustsFont;
    
    //其他
    btn.contentEdgeInsets = tag.padding;
    btn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    return btn;
}

+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

+ (UIColor *)darkerColor:(UIColor *)color {
    CGFloat h, s, b, a;
    if ([color getHue:&h saturation:&s brightness:&b alpha:&a])
        return [UIColor colorWithHue:h
                          saturation:s
                          brightness:b * 0.88
                               alpha:a];
    return color;
}

//- (void)setHighlighted:(BOOL)highlighted {
//    //不需要在方法里面做任何事情,这表示我们阻止了系统按钮默认的做法,屏蔽了它的高亮效果.按钮也只需简单地设置为:
//    //[button setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
//    //[button setImage:[UIImage imageNamed:@"like_selected"] forState:UIControlStateSelected];
//    //链接：https://www.jianshu.com/p/57b2c41448bf
//
//}

@end
